package com.bgIntlgn.client.entity.jishubu;

import yuhuibear.poat.annotations.action.AutoGenerateValue;
import yuhuibear.poat.annotations.action.Macro;
import yuhuibear.poat.annotations.relation.Field;
import yuhuibear.poat.annotations.relation.Table;
import yuhuibear.poat.object.bean.Bean;

/**
 * 零件分类实体(呈树形结构)
 * 
 * @author shenzhang
 * 
 */
@Table(name = "element_catalogs", constrain = "")
public class ElementCataloge implements Bean {

	public static String Dash = ">";

	/**
	 * 
	 */
	@AutoGenerateValue(skipOnNull = true, sqlToGet = "select currval('element_catalogs_id_seq')")
	@Field(name = "id", type = "bigserial", constrain = "primary key")
	private Long id;

	/**
	 * 父节点ID
	 */
	@Field(name = "parent_id", type = "int8", constrain = "references element_catalogs(id) on update cascade on delete cascade")
	private Long parentId;

	/**
	 * 指向某个具体产品， 组件或者零件。<br>
	 * <ul>
	 * <li>位于根时， 表示得是某个东西的一个版本下的一个组成。</li>
	 * <li>位于非根时， 表示用来构成其父节点的东西的一个具体的被引用的版本。</li>
	 * </ul>
	 */
	@Field(name = "uid", type = "char(80)", constrain = " not null references element_subversions(uid) on update cascade  on delete cascade")
	private String uid;

	/**
	 * 节点所处的层次级别
	 */
	@Field(name = "level", type = "int8", constrain = "default 0 ")
	private Long level;

	/**
	 * 从根节点到当前节点的路径信息,方便用户查找
	 */
	// "generate_cataloge_path",
	// sql =
	// "create trigger generate_cataloge_path_trigger after update or insert on element_catalogs for each row execute procedure generate_cataloge_path();")
	@Field(name = "path", type = "varchar(20)", constrain = "")
	private String path;

	/**
	 * 构成数量， 根为0, 零件自身在不被引用的时候， 其数量就为0.
	 */
	@Field(name = "quantity", type = "int", constrain = "not null default 1")
	private int quantity;

	/**
	 * 查找子类的ElementCataloge条目的方法。
	 * 
	 * @return
	 */
	@Macro(name = "list_element_cataloge", depend = "")
	public String subElement() {
		String sql = "create or replace function list_element_cataloge(uidtxt text) returns setof element_catalogs  as $body$ "
				+ " declare "
				+ "r element_catalogs; "
				+ " begin "
				+ "	select * into r from element_catalogs where element_catalogs.uid = uidtxt and element_catalogs.parent_id is null;"
				+ "	if not found then return; end if;"
				+ "	return next r;"
				+ "	while true"
				+ "	loop"
				+ "		select * into r from  element_catalogs where element_catalogs.parent_id = r.id;"
				+ "		if not found then return; end if;"
				+ "		return next r;"
				+ "	end loop;"
				+ " end" + " $body$ language plpgsql;";
		return sql;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Long getLevel() {
		return level;
	}

	public void setLevel(Long level) {
		this.level = level;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getUid() {
		return this.uid;
	}

	public void setUid(String uid) {
		this.uid = uid.trim();
	}

	public int getQuantity() {
		return this.quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

}
